package com.valkyries.jhhj.utils;

import cn.dev33.satoken.exception.*;
import com.valkyries.jhhj.common.Result;
import com.valkyries.jhhj.exception.CaptchaErrorException;
import com.valkyries.jhhj.exception.CaptchaTimeOutException;
import com.valkyries.jhhj.exception.PasswordErrorException;
import com.valkyries.jhhj.exception.UserNameNotExistException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理
 */

@Slf4j
@RestControllerAdvice
public class GlobalException {

    /**
     * 拦截：验证码过期
     * */
    @ExceptionHandler(CaptchaTimeOutException.class)
    public Result handlerException(CaptchaTimeOutException e){
        log.error(e.getMessage());
        return Result.error(e.getMessage());
    }

    /**
     * 拦截：验证码错误
     * */
    @ExceptionHandler(CaptchaErrorException.class)
    public Result handlerException(CaptchaErrorException e){
        log.error(e.getMessage());
        return Result.error(e.getMessage());
    }

    /**
     * 拦截：用户名不存在
     * */
    @ExceptionHandler(UserNameNotExistException.class)
    public Result handlerException(UserNameNotExistException e){
        log.error(e.getMessage());
        return Result.error(e.getMessage());
    }

    /**
     * 拦截：密码错误
     * */
    @ExceptionHandler(PasswordErrorException.class)
    public Result handlerException(PasswordErrorException e){
        log.error(e.getMessage());
        return Result.error(e.getMessage());
    }

    /**
     * 拦截：未登录异常
     */
    @ExceptionHandler(NotLoginException.class)
    public Result handlerException(NotLoginException e) {
        log.error(e.getMessage());
        return Result.error(e.getMessage());
    }

    /**
     * 拦截：缺少权限异常
     */
    @ExceptionHandler(NotPermissionException.class)
    public Result handlerException(NotPermissionException e) {
        log.error(e.getMessage());
        return Result.error("缺少权限: " + e.getPermission());
    }

    /**
     * 拦截：缺少角色异常
     */
    @ExceptionHandler(NotRoleException.class)
    public Result handlerException(NotRoleException e) {
        log.error(e.getMessage());
        return Result.error("缺少角色: " + e.getRole());
    }

    /**
     * 拦截：二级认证校验失败异常
     */
    @ExceptionHandler(NotSafeException.class)
    public Result handlerException(NotSafeException e) {
        log.error(e.getMessage());
        return Result.error("二级认证校验失败：" + e.getService());
    }


    /**
     * 拦截：服务封禁异常
     */
    @ExceptionHandler(DisableServiceException.class)
    public Result handlerException(DisableServiceException e) {
        log.error(e.getMessage());
        return Result.error("当前账号 " + e.getService() + " 服务已被封禁 (level=" + e.getLevel() + ")：" + e.getDisableTime() + "秒后解封");
    }

    /**
     * 拦截：Http Basic 校验失败异常
     */
    @ExceptionHandler(NotBasicAuthException.class)
    public Result handlerException(NotBasicAuthException e) {
        log.error(e.getMessage());
        return Result.error(e.getMessage());
    }

    /**
     * 拦截：其它异常
     */
    @ExceptionHandler(Exception.class)
    public Result handlerException(Exception e) {
        log.error(e.getMessage());
        return Result.error(e.getMessage());
    }

}
